package com.bzchao.netty.udp.config;

import com.bzchao.netty.udp.consumer.StringUdpConsumer;
import com.bzchao.netty.udp.consumer.UdpConsumer;
import com.bzchao.netty.udp.server.UdpEventServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.nio.charset.Charset;

@Slf4j
@Configuration
@ComponentScan("com.bzchao.netty.udp")
public class UdpConfig {

    /**
     * 配置udp消息的消费者(接收者)
     */
    @Bean
    @Order(101)
    @ConditionalOnMissingBean(UdpEventServer.class)
    public UdpEventServer udpEventServer() {
        log.debug(s"create default udpEventServer");
        UdpEventServer udpEventServer = new UdpEventServer();
        return udpEventServer;
    }

    /**
     * 配置udp消息的消费者
     * 对于实际的业务，只需要实现UdpConsumer或StringUdpConsumer即可
     */
    @Bean
    @Order(100)
    @ConditionalOnMissingBean(UdpConsumer.class)
    public UdpConsumer udpConsumer() {
        log.warn("create default udpConsumer");
        StringUdpConsumer udpConsumer = new StringUdpConsumer();
        udpConsumer.setRevCharset(Charset.forName("utf-8"));
        return udpConsumer;
    }

}
